1 module hip.image_backend.gamut;
2 import hip.api.data.image;
3 
4 version(HipGamutImageDecoder):
5 final class HipGamutImageDecoder : IHipAnyImageDecoder
6 {
7     import gamut;
8     Image img;
9     string path;
10     this(string path = "")
11     {
12         this.path = path;
13     }
14     bool startDecoding(ubyte[] data, void delegate() onSuccess, void delegate() onFailure)
15     {
16         img.loadFromMemory(data, LOAD_RGB | LOAD_ALPHA | LOAD_8BIT);
17         if(img.isValid)
18         {
19             img.changeLayout(LAYOUT_GAPLESS | LAYOUT_VERT_STRAIGHT);
20             onSuccess();
21         }
22         else
23             onFailure();
24         return img.isValid;
25     }
26 
27     uint getWidth() const
28     {
29         if(img.isValid)
30             return img.width;
31         return 0;
32     }
33 
34     uint getHeight() const
35     {
36         if(img.isValid)
37             return img.height;
38         return 0;
39     }
40 
41     const(ubyte[]) getPixels()  const
42     {
43         if(img.isValid)
44             return img.allPixelsAtOnce();
45         return null;
46     }
47 
48     ubyte getBytesPerPixel() const
49     {
50         final switch(img.type) with(PixelType)
51         {
52             case l8: return 1;
53             case l16: return 2;
54             case lf32: return 4;
55             case la8: return 2;
56             case la16: return 4;
57             case laf32: return 8;
58             case rgb8: return 3;
59             case rgb16: return 6;
60             case rgbf32: return 12;
61             case rgba8: return 4;
62             case rgba16: return 8;
63             case rgbaf32: return 16;
64             case unknown: assert(false, "Invalid image?");
65         }
66     }
67     ///Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
68     ubyte[] getPalette() const{return null;}
69 
70     void dispose()
71     {
72         destroy(img);
73     }
74 }